<?php

class GroupController extends ControllerModuleWhm
{
	public function actionIndex()
	{
		$groupAddForm = new GroupAddForm();
		$group = new Group();
		
		$postData = app()->request->getPost('GroupAddForm', null);
		if($postData){
			$groupAddForm->attributes = $postData;
			$ret = false;
			$detail = $errors = null;
			if($groupAddForm->validate()){
				$group->attributes = $postData;
				if(!($ret = $group->save(true))){
					$errors = $group->getErrors();
				}else{
					app()->__Whatever__Log->recordGroupAction(SystemLog::GROUP_ADD, $group->name, $group->id_group);
					
					$detail = $postData;
					$detail['id_group'] = $group->getPrimaryKey();
				}
			}else{
				$errors = $groupAddForm->getErrors();
			}
			
			$this->_returnAjax(
				array(
					'result' => $ret,
					'detail' => $detail,
					'errors' => $errors
				)
			);
		}
		
		$this->render('index', array(
			'groupAddForm' => $groupAddForm,
			'groupEditForm' => new GroupEditForm(),
			'group' => $group
		));
	}
	
	public function actionDetail($id_group)
	{
		$id_group = app()->request->getParam('id_group', null);
		if($id_group){
			if(app()->request->isAjaxRequest){
				$group = $this->loadModelByPk($id_group, 'Group');
				$this->_returnAjax(
					$group->attributes
				);
			}
		}
	}
	
	public function actionEdit()
    {
        $postData = app()->request->getParam('GroupEditForm', null);
    	if($postData){
//    		$postData = array_filter($postData);
	        $model = $this->loadModelByPk(empty($postData['id_group'])? '' : $postData['id_group'], 'Group');
	
	        if($model){
	            $model->attributes = $postData;
	            if(app()->request->isAjaxRequest){
	            	if(app()->user->hasGroupAccess($postData['id_group'])){
	            		$ret = $model->save();
	            		if($ret){
	            			app()->__Whatever__Log->recordGroupAction(SystemLog::GROUP_EDIT, $model->name, $model->id_group);
	            		}
	            		
		            	$this->_returnAjax(array(
		            		'result' => $ret,
		            		'detail' => $model->attributes,
		            		'errors' => $model->getErrors()
		            	));
	            	}else{
	            		$model->addError('Access', 'You do not have access to this group!');
	            		$this->_returnAjax(array(
		            		'result' => false,
		            		'errors' => $model->getErrors()
		            	));
	            	}
	            }else{
	            	if($model->save()){
	                	// redirect
	            	}
	            }
	        }
	
	        $this->render('edit', array(
	            'model' => $model,
	        ));    		
    	}
    }
	
	public function actionDelete()
	{
		if(app()->request->isAjaxRequest){
			$id_group = app()->request->getPost('id_group', null);
			if($id_group){
				$group = $this->loadModelByPk($id_group, 'Group');
				if(app()->user->hasGroupAccess($id_group)){
					$ret = $group->delete();
					if($ret){
						app()->__Whatever__Log->recordGroupAction(SystemLog::GROUP_DELETE, $group->name, $group->id_group);
					}
					$this->_returnAjax(array(
						'result' => $ret,
						'errors' => $group->getErrors()
					));
				}else{
					$group->addError('Access', 'You do not have access to this group');
					$this->_returnAjax(array(
						'result' => false,
						'errors' => $group->getErrors()
					));
				}
			}
		
			$this->_end();
		}
	}
	
	public function accessRules()
	{
		return array(
			array(
				'allow',
				'actions' => array('index', 'detail', 'edit', 'delete'),
				'expression' => 'app()->user->isLogin() && app()->user->hasManageGroupPerm()',
				'redirect' => array('default/noaccess')
			),

			array(
				'deny',	//should be 'deny', 'allow' is used to give convenient to make test
				'users' => array('*'),
				'redirect' => array('user/login')
			)
		);
	}
	
}